Generate the lock file when there is no lock file
authorsrinivasreddy <thatiparthysreenivas@gmail.com>
Sun, 20 Mar 2016 20:12:52 +0000 (01:42 +0530)
committerSrinivas Reddy Thatiparthy <thatiparthysreenivas@gmail.com>
Thu, 31 Mar 2016 04:05:59 +0000 (09:35 +0530)
src/cargo/ops/cargo_generate_lockfile.rs
tests/test_cargo_generate_lockfile.rs

index 1b8bb0087f0299e149d854198de73521c673298e..73fc36685376f552606d954401cd711978d7aaa6 100644 (file)
@@ -29,18 +29,20 @@ pub fn generate_lockfile(manifest_path: &Path, config: &Config)
 
 pub fn update_lockfile(manifest_path: &Path,
                        opts: &UpdateOptions) -> CargoResult<()> {
-    let package = try!(Package::for_path(manifest_path, opts.config));
-
-    let previous_resolve = match try!(ops::load_pkg_lockfile(&package,
-                                                             opts.config)) {
-        Some(resolve) => resolve,
-        None => bail!("a Cargo.lock must exist before it is updated")
-    };
 
     if opts.aggressive && opts.precise.is_some() {
         bail!("cannot specify both aggressive and precise simultaneously")
     }
 
+    let package = try!(Package::for_path(manifest_path, opts.config));
+
+    let previous_resolve = match try!(ops::load_pkg_lockfile(&package, opts.config)) {
+       Some(resolve) => resolve,
+       None => {
+            let _ = generate_lockfile(manifest_path, opts.config);
+            return Ok(());
+       }
+    };
     let mut registry = PackageRegistry::new(opts.config);
     let mut to_avoid = HashSet::new();
 
index c8a9bb6a274151312fd709990a906eccec716250..39ea2af31fe36caf2a43e551ea787849d8b2951e 100644 (file)
@@ -1,8 +1,8 @@
-use std::fs::File;
+use std::fs::{self, File};
 use std::io::prelude::*;
 
 use support::{project, execs};
-use hamcrest::{assert_that, existing_file};
+use hamcrest::{assert_that, existing_file, is_not};
 
 fn setup() {}
 
@@ -171,3 +171,26 @@ test!(preserve_line_endings_issue_2076 {
     assert!(lock2.starts_with("[root]\r\n"));
     assert_eq!(lock1, lock2);
 });
+
+test!(cargo_update_generate_lockfile {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            authors = []
+            version = "0.0.1"
+        "#)
+        .file("src/main.rs", "fn main() {}");
+
+    let lockfile = p.root().join("Cargo.lock");
+    assert_that(&lockfile, is_not(existing_file()));
+    assert_that(p.cargo_process("update"), execs().with_status(0).with_stdout(""));
+    assert_that(&lockfile, existing_file());
+
+    fs::remove_file(p.root().join("Cargo.lock")).unwrap();
+
+    assert_that(&lockfile, is_not(existing_file()));
+    assert_that(p.cargo("update"), execs().with_status(0).with_stdout(""));
+    assert_that(&lockfile, existing_file());
+
+});